<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">蜂巢贷运营管理系统</h3>
      </div>

      <el-form-item prop="username" v-if="showType === 1">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password"  v-if="showType === 1">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

    <!--找回密码区域-->
      <el-form-item prop="username"  v-if="showType === 2">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" auto-complete="on" />
      </el-form-item>

      <el-form-item prop="code"  v-if="showType === 2"> 
     <span class="svg-container">
          <svg-icon icon-class="code" />
        </span>
    <el-input v-model="loginForm.code" placeholder="请输入验证码" style="width:50%"></el-input>
    <el-button icon="el-icon-mobile-phone" @click="send" type="primary" style="width:40%" :disabled="disabled=!show" >  
        <span v-show="show">获取验证码</span>
        <span v-show="!show" class="count">{{count}} s</span>
     </el-button>
    </el-form-item>

      <el-form-item prop="password"  v-if="showType === 2">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
   

      <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" v-if="showType === 1" @click.native.prevent="handleLogin">登录</el-button>
      <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" v-if="showType === 2" @click.native.prevent="handleSubmit">提交</el-button>
        <el-link type="primary"  v-if="showType === 1" @click="showType=2" style="margin-left:40%;margin-bottom:10%;">忘记密码?</el-link>
        <el-link type="primary"  v-if="showType === 2" @click="showType=1" style="margin-left:40%;margin-bottom:10%;">账号密码登录</el-link>
    
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate';
import { resetPwd,sendCode } from '@/api/visible'
import { mapGetters } from 'vuex'
const TIME_COUNT = 60; //更改倒计时时间
const pattern = /^[\w_-]{6,16}$/;
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   callback(new Error('Please enter the correct user name'));
      // } else {
      //   callback();
      // }
    };
    const validatePassword = (rule, value, callback) => {
      if( value === undefined ){
          callback(new Error('请输入密码'));
      }else if (value.length < 6) {
        callback(new Error('密码必须6位以上'));
      }
      // else if(pattern.test(value)){
      //   callback(new Error('密码'));
      // }
       else {
        callback();
      }
    };
    return {
      loginForm: {
        username: undefined,
        password: undefined,
        code: undefined
      },
      showType: 1,//1显示登录区域 2显示找回密码
      show: true,  // 初始启用按钮
      count: '',   // 初始化次数
      timer: null,
      loginRules: {
        username: [{ required: true, trigger: 'blur',message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur',message: '请输入验证码' }],
      },
      loading: false,
      passwordType: 'password',
      redirect: '/',
    };
  },
  computed: {
    ...mapGetters(['router']),
  },
  watch: {
    $route: {
      handler: function (route) {
        /**
         * 路由变化的时候获取redirect参数，登录之后重定向到这个位置
         */
        this.redirect = route.query && route.query.redirect;
        console.log('redirect=' + this.redirect);
      },
      immediate: true,
    },
  },
  methods: {
        send(){
            if( this.loginForm.username === undefined ) {
              this.$message.error("请输入手机号")
              return false
            }
            if (!this.timer) {
           sendCode({phone: this.loginForm.username,templateType: "200"})
            .then(response => {
                this.$notify.success({
                title: '成功',
                message: '发送成功'
              })
              this.count = TIME_COUNT;
              this.show = false;
              this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= TIME_COUNT) {
                this.count--;
                } else {
                this.show = true;
                clearInterval(this.timer);  // 清除定时器
                this.timer = null;
                }
            }, 1000)
            })
            .catch(response => {
            })
              }
        },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              //path: this.redirect || '/'
              this.$router.push({ path: this.router });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleSubmit() {
      
      this.$refs['loginForm'].validate(valid => {
        if (valid) {
          resetPwd(this.loginForm)
            .then(response => {
                this.$notify.success({
                title: '成功',
                message: '密码重置成功'
              })
              this.showType = 1
            
            })
            .catch(response => {
            })
        }
      })
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
// $light_gray: rgb(63, 62, 62);
$light_gray: black;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #3b3d41;
$dark_gray: #889aa4;
$light_gray: #283443;

.login-container >>> .el-input__inner{
  color: #889aa4;
}

.login-container {
  // min-height: 100%;
  // width: 100%;
  // background-image: url('@/assets/404_images/login/v2_qq7esx.png') no-repeat;
  // background-size:100% 100%;
  // overflow: hidden;
    width: 100%;
  height: 100%;
  background-image: url("../../assets/login/v2_qq7esx.png");
  background-size: cover;
  background-position: bottom;
  position: relative;

  .login-form {
    position: relative;
    width: 23%;
    max-width: 100%;
    left: 67%;
    top: 27%;
    background-color: #fff;
    padding: 50px 30px 0;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 9px 10px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 5px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
